/**
 * Created by YongX on 2016/12/31.
 */
let fieldName   = null;
let body = $('body');
let thisId = window.location.hash;
$(document).ready(function () {
    body.css("background-color", "whitesmoke");
    body.css("height", "10px");
    $('.popover-with-html').hover(function () {
        $('.popover-with-html').popover({
            html : true,
            trigger : 'hover',
            container: 'body',
            placement: 'auto top'
        });
    });
    $('#uploadImg').change(function () {
        var formData = new FormData();
        formData.append('file', $(this)[0].files[0]);
        $.ajax({
            type: "post",
            url: "/upload/image",
            data: formData,
            cache: false,
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            contentType: false,
            processData: false,
            success: function (data) {
                if (fieldName != null){
                    fieldName.value = $('#cdn').val()+data.url;
                }
            },
            error: function (e) {
            }
        })
    });
    $('a[href*=#],area[href*=#]').click(function() {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            scrollMaodian($(this));
            return false;
        }
    });
    if (thisId != ''){
        scrollMaodian($('a[href='+thisId+']'));
    }
    $('i').click(function () {
        let  $this = $(this);
        if ($this.hasClass('fa')){
            $this.addClass('rubberBand animated');
            setTimeout(function () {
                $this.removeClass('rubberBand animated');
            },1000);
        }
    });
    $('#vote').click(function () {
        $.post("/topic/vote", {topic_id:$('#topic_id').val(),_token:$('meta[name=csrf-token]').attr('content')},function (data) {
            if (data.code == 0){
                if (data.type == 1){
                    $('.user-lists')
                        .append('<a href="/user/info/'+data.msg.uid+'" data-userid="'+data.msg.uid+'"" >' +
                            '<img class="img-thumbnail avatar avatar-middle" src="'+$('#cdn').val()+data.msg.avatar+'" style="width:48px;height:48px;"></a></div>');
                    $('.vote-hint').hide();
                    setTimeout(function () {
                        $('a[data-userid='+data.msg.uid+']>img').addClass('animated swing');
                    },50);
                }else{
                    $('a[data-userid='+data.msg.uid+']>img').addClass('animated bounceOut');
                    setTimeout(function () {
                        $('a[data-userid='+data.msg.uid+']').remove();
                    },800);
                }
            }else{
                notificationMsg(data.msg, 'bar', 'slidetop', 'danger', 'bottomright');
            }
        });
    });
});
function scrollMaodian(me) {
    let $target = $(me[0].hash);
    if ($target.selector == '' || $target.selector == null){
        return false;
    }
    $target = $target.length && $target || $('[name=' + me[0].hash.slice(1) + ']');
    if ($target.length) {
        let targetOffset = $target.offset().top - 100;
        $('html,body').animate({scrollTop: targetOffset},500);
        setTimeout(function () {window.location.hash = me.attr('href');},100);
    }
}

function voteReply($id, $this) {
    postMe('/topic/reply/vote', $id, function (data) {
        let span = $('span', $($this));
        $(span[0]).text(data.msg);
    });
}
function deleteReply($id, $this) {
    postMe('/topic/delete', $id, function (data) {
        $li = $($this).parents('li');
        $li.addClass('bounceOut animated');
        setTimeout(function () {
            $li.remove();
        },700);
    });
}
function postMe($url, $id, u) {
    $.post($url, {id:parseInt($id),_token:$('meta[name=csrf-token]').attr('content')},function (data) {
        if (data.code == 0){
            u(data);
        }else{
            notificationMsg(data.msg, 'bar', 'slidetop', 'danger', 'bottomright');
        }
    });
}
function notificationMsg($msg, $layout, $effect, $type, $position) {
    let notification = new NotificationFx({
        message: '<span class="icon pe-7s-bell"></span><p class="text-center">'+$msg+'</p>',
        layout: $layout,
        effect: $effect,
        type: $type,
        position: $position,
        onClose: function() {
        }
    });
    notification.show();
}